<template>
  <view>
    <z-paging
      ref="paging"
      style="background-color: #eeeeef"
      v-model="itemList"
      @query="queryList"
      :fixed="true"
      :auto="false"
      :auto-show-back-to-top="true"
      :empty-view-img="`${getImageUrl('empty.png')} `"
      empty-view-text="暂无数据~"
      :empty-view-img-style="imgStyle"
    >
      <view slot="top">
        <u-navbar title="美食外卖" title-size="36" :border-bottom="false">
          <view class="slot-wrap">
            <view style="width: 690rpx; overflow: auto">
              <view class="location dis-flex flex-y-center" @click="onCity">
                <u-icon
                  style="vertical-align: middle; margin-right: 10rpx"
                  name="/static/dw-g.png"
                  size="30rpx"
                  mode=""
                ></u-icon>
                <text class="address u-line-1">{{
                  address.cityName ? subCity(address.cityName) : '选择'
                }}</text>
                <u-icon
                  style="margin-left: 10rpx"
                  name="arrow-down"
                  color="#333333"
                  size="26"
                ></u-icon>
              </view>
            </view>
          </view>
        </u-navbar>
        <view class="searchView">
          <u-search
            placeholder="搜索所需商品"
            height="64"
            @search="onSearch"
            v-model="keywords"
            :show-action="true"
            style="width: 100%"
          ></u-search>
        </view>
      </view>

      <view class="card service">
        <scroll-view
          class="cate-left"
          :scroll-x="true"
          :show-scrollbar="false"
          :enhanced="true"
          @touchmove.stop.prevent
        >
          <view
            class="item"
            @click="routeToCate(service)"
            v-for="(service, index) in categoryList"
            :key="index"
          >
            <u-image shape="circle" :src="service.iconUrl" width="90rpx" height="90rpx"></u-image>
            <view class="name"> {{ service.name }}</view>
          </view>
        </scroll-view>
      </view>
      <view class="shaixun-box">
        <view class="sx-l first-item" style="left: 0">
          <view
            class="sx-item"
            :style="sortType == '' ? 'color:#10A28F;' : ''"
            @click="onSortField('')"
          >
            <text>综合排序</text>
          </view>
        </view>
        <view class="sx-l">
          <view
            class="sx-item mar-20"
            @click="onSortField('sales_count')"
            :style="sortType == 'sales_count' ? 'color:#10A28F;' : ''"
          >
            <text>销量高</text>
          </view>
          <view
            class="sx-item"
            @click="onSortField('distance')"
            :style="sortType == 'distance' ? 'color:#10A28F;' : ''"
          >
            <text>距离近</text>
          </view>
        </view>
        <u-dropdown ref="uDropdown" @open="open" @close="close" active-color="#10A28F">
          <u-dropdown-item></u-dropdown-item>
          <u-dropdown-item
            v-model="value2"
            title="全部筛选"
            @change="changeItem"
            :options="options2"
          ></u-dropdown-item>
        </u-dropdown>
      </view>

      <view class="goods_area">
        <StoreBox :itemList="itemList"></StoreBox>
      </view>
    </z-paging>
  </view>
</template>

<script>
import utils from '@/common/utils'
import StoreBox from '@/components/store_box.vue'
export default {
  components: {
    StoreBox
  },
  data() {
    return {
      value1: 1,
      value2: 2,
      options1: [],
      keywords: '',
      options2: [
        {
          label: '人气优先',
          value: 'browse_count'
        },
        {
          label: '评分优先',
          value: 'score'
        }
      ],
      show: false,
      categoryList: [],
      itemList: [],
      city: '',
      imgStyle: {
        width: '12rem'
      },
      sortField: null,
      sortType: 'random',
      address: {
        name: '',
        code: ''
      }
    }
  },
  onLoad() {
    this.address = uni.getStorageSync('address') || {}
    this.queryList(1)
    this.$u
      .get('/app-api/shop/category/list', {
        parentCode: 'food'
      })
      .then((res) => {
        this.categoryList = res.data
      })
  },

  methods: {
    subCity(city) {
      return city.length > 4 ? city.substr(0, 4) + '...' : city
    },
    onCity() {
      uni.$on('selectAddress', (res) => {
        this.address = uni.getStorageSync('address') || {}
        this.queryList(1)
        uni.$off('selectAddress')
      })
      uni.navigateTo({
        url: '/other/select-city/select-city'
      })
    },
    routeTo(url) {
      uni.navigateTo({
        url: url
      })
    },

    routeToCate(item) {
      let query = utils.urlEncode(item)

      uni.navigateTo({
        url: '/lianmeng/store-list-waimai-cate?' + query
      })
    },
    onSortField(type) {
      this.sortType = type
      // default、random、score、price
      this.$refs.uDropdown.highlight()
      this.queryList(1)
    },
    changeItem(res) {
      this.sortType = res
      this.queryList(1)
    },
    onSearch() {
      console.log('this.keywords', this.keywords)
      this.queryList(1)
    },
    queryList(pageNo = 1, pageSize = 10) {
      if (pageNo === 1) {
        this.itemList = []
      }
      this.$u.api
        .getStoreList({
          pageNo,
          pageSize,
          cityCode: this.address.code,
          sortType: this.sortType,
          categoryCode: 'food',
          keyword: this.keywords
        })
        .then((res) => {
          this.$refs.paging.complete(res.data.list)
        })
    },
    open(index) {
      // 展开某个下来菜单时，先关闭原来的其他菜单的高亮
      // 同时内部会自动给当前展开项进行高亮
      this.$refs.uDropdown.highlight()
      this.sortField = null
    },
    close(index) {
      // 关闭的时候，给当前项加上高亮
      // 当然，您也可以通过监听dropdown-item的@change事件进行处理
      this.$refs.uDropdown.highlight(index)
    }
  }
}
</script>

<style scoped lang="scss">
page {
  background-color: #f0f0f0;
}

.address {
  font-size: 30rpx;
}

.searchView {
  padding: 13rpx 20rpx;
  background-color: #fff;
}

.card {
  margin: 20rpx;
  background-color: #fff;
  border-radius: 10rpx;
}

.service {
  padding: 20rpx 0;

  // width: 100%;
  .cate-left {
    white-space: nowrap;
    width: 100%;
    box-sizing: border-box;
    height: 100%;
  }

  .item {
    height: 100%;
    display: inline-block;
    margin-right: 20rpx;
    text-align: center;

    .name {
      font-size: 26rpx;
      color: #333333;
    }
  }

  .item:first-child {
    margin-left: 20rpx;
  }
}

.goods_area {
  .goods_box {
    /* width: 100%; */
    background: #ffffff;
    border-radius: 10rpx;
    padding: 20rpx;
    margin: 0 20rpx;
    margin-bottom: 20rpx;
  }

  .store-image {
    position: relative;
  }

  .store-label {
    position: absolute;
    top: 8rpx;
    right: 8rpx;
    width: 60rpx;
    height: 30rpx;
    background: #ed624f;
    border-radius: 10rpx;
    font-size: 20rpx;
    color: #ffffff;
    text-align: center;
    line-height: 30rpx;
  }

  .goods_title {
    font-size: 30rpx;
    color: #333333;
  }

  .laba {
    font-size: 20rpx;
    color: #999999;
    padding: 30rpx 0 10rpx 0;
  }

  .goods_price {
    font-size: 25rpx;
    color: red;
    display: flex;
  }

  .price_tip {
    font-size: 35rpx;
  }

  .sales-volume1 {
    color: #999999;
  }

  .price_main {
    font-size: 36rpx;
  }

  .price_dot {
    align-self: flex-end;
  }

  .goods_tip {
    text {
      width: auto;
      // height: 40rpx;
      border-radius: 20rpx;
      border: 1px solid #fa453c;
      // line-height: 40rpx;
      text-align: center;
      font-size: 20rpx;
      color: #fa453c;
      padding: 2rpx 20rpx;
    }
  }

  .goods_old_price {
    color: #89898a;
  }

  .storeName {
    font-size: 26rpx;
    font-weight: 400;
    color: #ffffff;
    line-height: 98px;
    margin-left: 10rpx;
    margin-right: 10rpx;
    margin-top: -4rpx;
  }

  .cabinetName {
    height: 30rpx;
    background: #ffffff50;
    border-radius: 5rpx;
    padding: 0px 10rpx;
    font-size: 22rpx;
    font-weight: 400;
    color: #ffffff;
    margin-right: 10rpx;
  }

  .goods-star {
    font-size: 30rpx;
    color: #333333;
    margin-left: 8rpx;
  }

  .sale-item {
    font-size: 24rpx;
    color: #666666;
    /* margin-right: 50rpx; */
  }
}

.hot {
  font-weight: 600;
  font-size: 32rpx;
  color: #333333;
  margin-bottom: 30rpx;
  padding-top: 10rpx;
  margin-left: 20rpx;
}

.shaixuan {
  padding: 0 20rpx 20rpx 20rpx;

  .sx-item {
    // width: 130rpx;
    height: 60rpx;
    background: #ffffff;
    border-radius: 10rpx;
    line-height: 60rpx;
    padding: 0 12rpx;
    font-size: 28rpx;
    color: #333333;
    z-index: 9999;
  }

  .mar-20 {
    margin-right: 20rpx;
  }
}

.shaixun-box {
  position: relative;
  padding: 0 20rpx;

  .sx-l {
    display: flex;
    position: absolute;
    top: 10rpx;
    left: 25%;
    justify-content: flex-start;
    width: 50%;
    z-index: 9999;

    .sx-item {
      height: 30px;
      background: #ffffff;
      border-radius: 5px;
      line-height: 30px;
      padding: 0 6px;
      font-size: 14px;
      color: #333333;
      margin-left: 20rpx;
    }
  }
}

::v-deep.shaixun-box .u-dropdown .u-dropdown__menu {
  justify-content: space-between;

  .u-dropdown__menu__item {
    width: 25%;
    flex: none;

    .u-flex {
      height: 30px;
      background: #ffffff;
      border-radius: 5px;
      line-height: 30px;
      padding: 0 6px;
      font-size: 14px;
      color: #333333;
    }
  }
}
</style>
